package com.jeesite.modules.dataScreen.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.jeesite.modules.dataScreen.service.DataScreenService;

import java.util.List;
import java.util.Map;

/**
 * 大屏查询工具类
 *
 * @author lh
 * @version 2023年5月6日
 */
public class DataScreenUtils {

    // 系统缓存常量
    /**
     *
     */
    public static final String CACHE_DATA_SCREEN_ALL_LIST = "dataScreenAllList";
    /**
     *
     */
    public static final String CACHE_DATA_SCREEN_LIST_BY_KEYS = "dataScreenListByKeys";

    /**
     * 可视化大屏动态数据
     */
    public static final String CACHE_NAME_SCREEN_DYNAMIC = "DATA_SCREEN_DYNAMIC";

    /**
     * 可视化大屏列表数据
     */
    public static final String CACHE_NAME_SCREEN_LIST = "CACHE_NAME_SCREEN_LIST";

    /**
     * 公共交通-当日公交客流量
     */
    public static final String CACHE_KEY_CURRENT_PASSENGER_FLOW_BUS_DATA = "CURRENT_PASSENGER_FLOW_BUS_DATA";
    /**
     * 运行动态-当前路网车流量
     */
    public static final String CACHE_KEY_CURRENT_NETWORK_FLOW_DATA = "CURRENT_NETWORK_FLOW_DATA";
    /**
     * 运行动态-当天高速车流量
     */
    public static final String CACHE_KEY_CURRENT_HIGHWAY_FLOW_DATA = "CURRENT_HIGHWAY_FLOW_DATA";
    /**
     * 畅行高速-当天外省入渝车流量
     */
    public static final String CACHE_KEY_CURRENT_PROVINCIAL_FLOW_DATA = "CURRENT_PROVINCIAL_FLOW_DATA";

    /**
     * 运输服务-客运服务-水路客运运行信息-当年三峡游客运量
     */
    public static final String CACHE_KEY_YEAR_SANXIA_PASSENGER_DATA = "YEAR_SANXIA_PASSENGER_DATA";

    /**
     * 运输服务-客运服务-水路客运运行信息-当年朝天门两江游客运量
     */
    public static final String CACHE_KEY_YEAR_LIANGJIANG_PASSENGER_DATA = "YEAR_LIANGJIANG_PASSENGER_DATA";

    /**
     * 综合概览-运行动态-当天江北机场登机人数
     */
    public static final String CACHE_KEY_CURRENT_JIANGBEI_BOARD_DATA = "CURRENT_JIANGBEI_BOARD_DATA";

    /**
     * 综合概览-运行动态-当天江北机场起飞航班数
     */
    public static final String CACHE_KEY_CURRENT_JIANGBEI_FLIGHT_DATA = "CURRENT_JIANGBEI_FLIGHT_DATA";

    /**
     * 综合概览-运行动态-年度累计江北机场登机人数
     */
    public static final String CACHE_KEY_YEAR_JIANGBEI_BOARD_DATA = "YEAR_JIANGBEI_BOARD_DATA";

    /**
     * 综合概览-运行动态-年度累计江北机场起飞航班数
     */
    public static final String CACHE_KEY_YEAR_JIANGBEI_FLIGHT_DATA = "YEAR_JIANGBEI_FLIGHT_DATA";

    /**
     * 综合概览-运行动态-当天三峡游客运量
     */
    public static final String CACHE_KEY_CURRENT_SANXIA_PASSENGER_DATA = "CURRENT_SANXIA_PASSENGER_DATA";

    /**
     * 综合概览-运行动态-当天朝天门两江游客运量
     */
    public static final String CACHE_KEY_CURRENT_LIANGJIANG_PASSENGER_DATA = "CURRENT_LIANGJIANG_PASSENGER_DATA";
    /**
     * 综合概览-运行动态-轨道交通客流量
     */
    public static final String CACHE_KEY_CURRENT_RAIL_TRANSIT_FLOW_DATA = "CURRENT_RAIL_TRANSIT_FLOW_DATA";

    /**
     * 获取所有大屏数据列表（系统级别缓存）
     *
     * @return 结果
     */
    public static List<Map<String, String>> getDataScreenAllList() {
        List<Map<String, String>> dataScreenList = SysCacheUtils.get(CACHE_DATA_SCREEN_ALL_LIST);
        if (dataScreenList == null) {
            dataScreenList = DataScreenUtils.Static.dataScreenService.getDataScreen();
            SysCacheUtils.put(CACHE_DATA_SCREEN_ALL_LIST, dataScreenList);
        }
        return dataScreenList;
    }

    /**
     * 根据关键字查询大屏数据
     *
     * @param key1 模块名
     * @param key2 子模块名
     * @param key3 子子模块名
     * @return 结果
     */
    public static List<Map<String, String>> getDataScreenList(String key1, String key2, String key3) {
        List<Map<String, String>> dataScreenListByKeys = CacheUtils.get(CACHE_DATA_SCREEN_LIST_BY_KEYS, key1 + key2 + key3);
        if (CollUtil.isEmpty(dataScreenListByKeys)) {
            dataScreenListByKeys = CollUtil.newArrayList();
            List<Map<String, String>> dataScreenList = SysCacheUtils.get(CACHE_DATA_SCREEN_ALL_LIST);
            if (dataScreenList == null) {
                dataScreenList = DataScreenUtils.Static.dataScreenService.getDataScreen();
                SysCacheUtils.put(CACHE_DATA_SCREEN_ALL_LIST, dataScreenList);
            }
            for (Map<String, String> map : dataScreenList) {
                String key1Copy = map.get("key1");
                String key2Copy = map.get("key2");
                String key3Copy = map.get("key3");
                if (key1.equals(key1Copy) && key2.equals(key2Copy) && ((StrUtil.isBlank(key3) && StrUtil.isEmpty(key3Copy)) || key3.equals(key3Copy))) {
                    dataScreenListByKeys.add(map);
                }
            }
            CacheUtils.put(CACHE_DATA_SCREEN_LIST_BY_KEYS, key1 + key2 + key3, dataScreenListByKeys);
        }
        return dataScreenListByKeys;
    }

    /**
     * 清理大屏数据缓存
     */
    public static void clearCache() {
        SysCacheUtils.remove(CACHE_DATA_SCREEN_ALL_LIST);
        CacheUtils.removeCache(CACHE_DATA_SCREEN_LIST_BY_KEYS);
        CacheUtils.removeCache(CACHE_NAME_SCREEN_DYNAMIC);
        CacheUtils.removeCache(CACHE_NAME_SCREEN_LIST);
        CacheUtils.removeCache(CACHE_KEY_CURRENT_PASSENGER_FLOW_BUS_DATA);
        CacheUtils.removeCache(CACHE_KEY_CURRENT_NETWORK_FLOW_DATA);
        CacheUtils.removeCache(CACHE_KEY_CURRENT_HIGHWAY_FLOW_DATA);
        CacheUtils.removeCache(CACHE_KEY_CURRENT_PROVINCIAL_FLOW_DATA);
    }

    /**
     * 静态内部类，延迟加载，懒汉式，线程安全的单例模式
     */
    private static final class Static {
        private static final DataScreenService dataScreenService = SpringUtils.getBean(DataScreenService.class);
    }

}
